<?php

class SettingController extends BackendController
{
    public $layout='/layouts/column2';
    public $city_en;
    public function actionRepassword(){
        if(Yii::app()->request->isPostRequest){

            $uid = Yii::app()->admin->id;
            $user = User::model()->findByPk($uid);
            if(!$user){
                $data['status'] = 0;
                $data['message']= '非法操作';
                echo json_encode($data);
                exit;
            }
            $now_password = Yii::app()->request->getParam('now_password');
            $password = Yii::app()->request->getParam('password');
            if($user->password !== sha1($now_password.$user->salt)){
                $data['status'] = 0;
                $data['message']= '密码错误';
                echo json_encode($data);
                Yii::app()->end();
            }

            $user->password = sha1($password.$user->salt);
            if($user->update()){
                $data['status']=1;
                $data['message']= '密码修改成功';
                echo json_encode($data);
                exit;
            }else{
                $data['status']=0;
                $data['message']= '密码修改失败';
                echo json_encode($data);
                exit;
            }
        }
        $this->render('repassword');
    }
    public function actionCity(){
        $model = new CityArea();
        //所属城市
        $city_id = Yii::app()->admin->getState('city');
        $this->city_en = Yii::app()->admin->getState('city_en');
        $dataProvider = $model->getList($city_id);
        $this->render('city',array('dataProvider'=>$dataProvider));
    }
    public function actionAddCity(){
        $city_id = Yii::app()->admin->getState('city');
        if(Yii::app()->request->isPostRequest){
            $_POST['Article']['status'] = 0;
            $_POST['Article']['city_id'] = $city_id;
            $model = new CityArea();
            $model->setAttributes($_POST['Article']);
            if($model->save()){
                    $data['status'] = 1;
                    $data['message'] = '添加区域成功';
            }else{
                $data['status'] = 0;
                $data['message'] = $this->showError($model->getErrors());
            }
            echo json_encode($data);
            Yii::app()->end();
        }else{
            $this->layout = '/layouts/dialog';
            Yii::app()->clientScript->registerScriptFile($this->adminPath . '/js/jquery.min.js',CClientScript::POS_HEAD);
            //获取区域
            $this->render('add_city');
        }
    }
    public function actionSetStatus(){
        $id = Yii::app()->request->getParam('id');
        $city_id = Yii::app()->admin->getState('city');
        $info = CityArea::model()->find('id=:id and city_id=:city_id',array(':id'=>$id,':city_id'=>$city_id));
        if($info){
            $info->status = 1;
            $ok = $info->update();
            if($ok){
                $data['status'] = 1;
                $data['message'] = '设为显示成功';
            }else{
                $data['status'] = 0;
                $data['message'] = '设为显示失败';
            }
        }else{
            $data['status'] = 0;
            $data['message'] = '你无权作此操作';
        }
        echo json_encode($data);
        Yii::app()->end();
    }
    public function actionSetUnStatus(){
        $id = Yii::app()->request->getParam('id');
        $city_id = Yii::app()->admin->getState('city');
        $info = CityArea::model()->find('id=:id and city_id=:city_id',array(':id'=>$id,':city_id'=>$city_id));
        if($info){
            $info->status = 0;
            $ok = $info->update();
            if($ok){
                $data['status'] = 1;
                $data['message'] = '取消显示成功';
            }else{
                $data['status'] = 0;
                $data['message'] = '取消显示失败';
            }
        }else{
            $data['status'] = 0;
            $data['message'] = '你无权作此操作';
        }
        echo json_encode($data);
        Yii::app()->end();
    }
    public function actionCityUpdate(){
        $id = Yii::app()->request->getParam('id');
        $city_id = Yii::app()->admin->getState('city');
        $info = CityArea::model()->find('id=:id and city_id=:city_id',array(':id'=>$id,':city_id'=>$city_id));
        if(Yii::app()->request->isPostRequest){
            $info->setAttributes($_POST['Article']);
            if($info->update()){
                $data['status'] = 1;
                $data['message'] = '修改区域成功';
            }else{
                $data['status'] = 0;
                $data['message'] = $this->showError($info->getErrors());
            }
            echo json_encode($data);
            Yii::app()->end();
        }else{
            $this->layout = '/layouts/dialog';
            Yii::app()->clientScript->registerScriptFile($this->adminPath . '/js/jquery.min.js',CClientScript::POS_HEAD);
            //获取区域
            $this->render('update_city',array('info'=>$info));
        }
    }
}